package com.lsp.base.utils.imageloader;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;

/**
 * Glide
 * Created by xcj on 16/5/21.
 */
public class GlideBuilder extends ImageLoaderBuilder {

  private Context mContext;

  private String mImageUrl;

  private WyImageOptions mWyImageOptions;

  private Object mTransform;

  private ImageLoaderListener mImageLoaderListener;

  @Override public ImageLoaderBuilder context(Context context) {
    mContext = context;
    return this;
  }

  @Override public ImageLoaderBuilder imageUrl(String imageUrl) {
    mImageUrl = imageUrl;
    return this;
  }

  @Override public ImageLoaderBuilder options(WyImageOptions options) {
    mWyImageOptions = options;
    return this;
  }

  @Override public ImageLoaderBuilder transform(Object transform) {
    mTransform = transform;
    return this;
  }

  @Override public ImageLoaderBuilder listen(ImageLoaderListener imageLoaderListener) {
    mImageLoaderListener = imageLoaderListener;
    return this;
  }

  @Override public void display(ImageView targetView) {
    RequestManager requestManager;
    if (mContext == null) {
      return;
    }
    if (mContext instanceof FragmentActivity) {
      requestManager = Glide.with((FragmentActivity) mContext);
    } else if (mContext instanceof Activity) {
      requestManager = Glide.with((Activity) mContext);
    } else {
      requestManager = Glide.with(mContext);
    }

    if (requestManager != null) {
      GlideOptions glideOptions = getGlideOptions(mWyImageOptions);
      final DrawableTypeRequest<String> request = requestManager.load(mImageUrl);
      if (mTransform != null && mTransform instanceof Transformation) {
        request.bitmapTransform((Transformation) mTransform);
      }
      if (mImageLoaderListener != null) {
        request.listener(new RequestListener<String, GlideDrawable>() {
          @Override
          public boolean onException(Exception e, String model, Target<GlideDrawable> target,
              boolean isFirstResource) {
            mImageLoaderListener.onFailed();
            return false;
          }

          @Override public boolean onResourceReady(GlideDrawable resource, String model,
              Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            mImageLoaderListener.onSucceed(resource);
            return false;
          }
        });
      }
      if (glideOptions == null) {
        request.diskCacheStrategy(DiskCacheStrategy.SOURCE).into(targetView);
      } else {
        request.diskCacheStrategy(DiskCacheStrategy.SOURCE)
            .placeholder(glideOptions.placeholder)
            .error(glideOptions.error)
            .fallback(glideOptions.fallback)
            .into(targetView);
      }
    }
  }

  @Override public void download(Object target) {
    RequestManager requestManager;
    if (mContext instanceof FragmentActivity) {
      requestManager = Glide.with((FragmentActivity) mContext);
    } else if (mContext instanceof Activity) {
      requestManager = Glide.with((Activity) mContext);
    } else {
      requestManager = Glide.with(mContext);
    }

    if (requestManager != null) {
      GlideOptions glideOptions = getGlideOptions(mWyImageOptions);
      DrawableTypeRequest<String> request = requestManager.load(mImageUrl);
      if (target instanceof SimpleTarget) {
        if (glideOptions == null) {
          //磁盘缓存策略 只缓存原图
          request.asBitmap()
              .diskCacheStrategy(DiskCacheStrategy.SOURCE)
              .into((SimpleTarget) target);
        } else {
          request.asBitmap()
              .diskCacheStrategy(DiskCacheStrategy.SOURCE)
              .placeholder(glideOptions.placeholder)
              .error(glideOptions.error)
              .fallback(glideOptions.fallback)
              .into((SimpleTarget) target);
        }
      }
    }
  }

  public class GlideOptions {
    public int placeholder;
    public int error;
    public int fallback;
  }

  public GlideOptions getGlideOptions(WyImageOptions options) {
    GlideOptions glideOptions = null;
    switch (options) {
      case NONE:
        break;
    }
    return glideOptions;
  }
}
